package presentation.playgameForm_UI;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;

import presentation.menuForm_UI.Picture;
import business.playgameForm_Evt.PlaySound;
import business.playgameForm_Evt.Playgame_Evt;

public class PlayGameForm_UI extends JDialog {

        private JPanel panelMain = new JPanel();
        private JPanel panelExplain = new JPanel();
        private JPanel panelButton = new JPanel();
        private Picture introPic = new Picture();
        
        private JButton btSinglePlayer = new JButton();
        private JButton btPractice = new JButton();
        private JButton btPlaySavedGame = new JButton();
        
        private Playgame_Evt event = new Playgame_Evt(this);
        
        private static final long serialVersionUID = 1L;
        
        private Picture pic = new Picture();
        
        private PlaySound sound = new PlaySound();
        
        boolean playSGCheck = false;
        boolean practiceCheck = false;
        boolean singleCheck = false;

        public PlayGameForm_UI(JFrame frame) {
                super(frame, true);
                initComponents();
                this.pack();
                this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                this.setLocationRelativeTo(frame);
                this.setResizable(false);
                this.setTitle("Play Game Mode");
                pic.path = "src/Image/focused_singleplayer.png";
                pic.repaint();
                btSinglePlayer.requestFocus();
        }

        public void initComponents() {
                setMainLayout();
                picPanelEvent();
        }

        private void setMainLayout() {
                setPanelExplainLayout();
                setPanelButtonLayout();
                this.add(panelMain);

                panelMain.setLayout(new FlowLayout(FlowLayout.CENTER, 0,0));
                panelMain.add(panelButton);
                panelMain.add(panelExplain);

                buttonEvent();
                windowEvent();
        }

        private void setPanelButtonLayout() {
                setButtons();
                panelButton.setLayout(null);
                panelButton.setPreferredSize(new Dimension(414, 439));
                pic.setBounds(0, 0, 414, 439);
                panelButton.add(pic);
                panelButton.setBorder(BorderFactory.createEtchedBorder());
                panelButton.add(btPractice);
                panelButton.add(btSinglePlayer);
                panelButton.add(btPlaySavedGame);
                btPractice.setBounds(0, 0, 10, 10);
                btSinglePlayer.setBounds(10, 10, 10, 10);
                btPlaySavedGame.setBounds(10, 10, 10, 10);

        }

        private void setPanelExplainLayout() {
        		panelExplain.setPreferredSize(new Dimension(414, 439));
                panelExplain.setLayout(null);
                
                introPic.path = "src/image/tooltips/single player.png";
                introPic.repaint();
                introPic.setBackground(panelExplain.getBackground());
                panelExplain.add(introPic);
                introPic.setBounds(0, 0, 414, 439);
        }

        private void setButtons() {
                btSinglePlayer.setOpaque(false);
                btSinglePlayer.setBorderPainted(false);
                btSinglePlayer.setContentAreaFilled(false);

                btPractice.setOpaque(false);
                btPractice.setBorderPainted(false);
                btPractice.setContentAreaFilled(false);

                btPlaySavedGame.setOpaque(false);
                btPlaySavedGame.setBorderPainted(false);
                btPlaySavedGame.setContentAreaFilled(false);
        }

        private void buttonEvent() {
                btSinglePlayer.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                                sound.mousePressed();
                                event.btSinglePlayer_Evt();
                        }
                });
                btPractice.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                                sound.mousePressed();
                                event.btPractice_Evt();
                        }
                });
                btPlaySavedGame.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                                sound.mousePressed();
                                event.btPlaySavedGame_Evt();
                        }
                });
        }

        private void windowEvent() {
                this.addWindowListener(new WindowAdapter() {

                        @Override
                        public void windowClosed(WindowEvent e) {
                                setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
                                event.showMainMenu();
                        }
                });

                btSinglePlayer.addKeyListener(new KeyAdapter() {

                        @Override
                        public void keyPressed(KeyEvent evt) {
                                if (evt.getKeyCode() == KeyEvent.VK_UP || evt.getKeyCode() == KeyEvent.VK_LEFT) {
                                        btPractice.requestFocus();
                                        pic.path = "src/Image/focused_practice.png";
                                        introPic.path = "src/image/tooltips/practice.png";
                                        introPic.repaint();
                                        pic.repaint();
                                       
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_RIGHT || evt.getKeyCode() == KeyEvent.VK_DOWN) {
                                        btPlaySavedGame.requestFocus();
                                        pic.path = "src/Image/focused_playsavedgame.png";
                                        introPic.path = "src/image/tooltips/Play saved game.png";
                                        introPic.repaint();
                                        pic.repaint();
                                       
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
                                        setVisible(false);
                                        event.showMainMenu();
                                }
                        }
                });

                btPractice.addKeyListener(new KeyAdapter() {

                        @Override
                        public void keyPressed(KeyEvent evt) {
                                if (evt.getKeyCode() == KeyEvent.VK_RIGHT || evt.getKeyCode() == KeyEvent.VK_DOWN) {
                                        btSinglePlayer.requestFocus();
                                        pic.path = "src/Image/focused_singleplayer.png";
                                        introPic.path = "src/image/tooltips/single player.png";
                                        introPic.repaint();
                                        pic.repaint();
                                       
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_LEFT || evt.getKeyCode() == KeyEvent.VK_UP) {
                                        btPlaySavedGame.requestFocus();
                                        pic.path = "src/Image/focused_playsavedgame.png";
                                        introPic.path = "src/image/tooltips/play saved game.png";
                                        introPic.repaint();
                                        pic.repaint();
                                       
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
                                        setVisible(false);
                                        event.showMainMenu();
                                }
                        }
                });

                btPlaySavedGame.addKeyListener(new KeyAdapter() {

                        @Override
                        public void keyPressed(KeyEvent evt) {
                                if (evt.getKeyCode() == KeyEvent.VK_LEFT || evt.getKeyCode() == KeyEvent.VK_UP) {
                                        btSinglePlayer.requestFocus();
                                        pic.path = "src/Image/focused_singleplayer.png";
                                        introPic.path = "src/image/tooltips/single player.png";
                                        introPic.repaint();
                                        pic.repaint();
                                       
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_RIGHT || evt.getKeyCode() == KeyEvent.VK_DOWN) {
                                        btPractice.requestFocus();
                                        pic.path = "src/Image/focused_practice.png";
                                        introPic.path = "src/image/tooltips/practice.png";
                                        introPic.repaint();
                                        pic.repaint();
                                       
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
                                        setVisible(false);
                                        event.showMainMenu();
                                }
                        }
                });
        }

        private void picPanelEvent() {
                pic.addMouseMotionListener(new MouseMotionAdapter() {

                        @Override
                        public void mouseMoved(MouseEvent evt) {
                                final int X = evt.getX();
                                final int Y = evt.getY();
                                if ((X >= 23 && X <= 23 + 137) && (Y >= 19 && Y <= 19 + 136)) {
                                        btPlaySavedGame.requestFocus();
                                        pic.path = "src/Image/focused_playsavedgame.png";
                                        introPic.path = "src/image/tooltips/play saved game.png";
                                        introPic.repaint();
                                        pic.repaint();
                                }
                                if ((X >= 181 && X <= 181 + 137) && (Y >= 118 && Y <= 118 + 136)) {
                                        btPractice.requestFocus();
                                        pic.path = "src/Image/focused_practice.png";
                                        introPic.path = "src/image/tooltips/practice.png";
                                        introPic.repaint();
                                        pic.repaint();
                                }
                                if ((X >= 263 && X <= 263 + 137) && (Y >= 282 && Y <= 282 + 136)) {
                                        btSinglePlayer.requestFocus();
                                        pic.path = "src/Image/focused_singleplayer.png";
                                        introPic.path = "src/image/tooltips/single player.png";
                                        introPic.repaint();
                                        pic.repaint();
                                }
                        }
                });

                pic.addMouseListener(new MouseAdapter() {

                        @Override
                        public void mousePressed(MouseEvent evt) {
                                final int X = evt.getX();
                                final int Y = evt.getY();
                                if ((X >= 23 && X <= 23 + 137) && (Y >= 19 && Y <= 19 + 136)) {
                                        btPlaySavedGame.requestFocus();
                                        sound.mousePressed();
                                        event.btPlaySavedGame_Evt();
                                }
                                if ((X >= 181 && X <= 181 + 137) && (Y >= 118 && Y <= 118 + 136)) {
                                        btPractice.requestFocus();
                                        sound.mousePressed();
                                        event.btPractice_Evt();
                                }
                                if ((X >= 263 && X <= 263 + 137) && (Y >= 282 && Y <= 282 + 136)) {
                                        btSinglePlayer.requestFocus();
                                        sound.mousePressed();
                                        event.btSinglePlayer_Evt();
                                }
                        }
                });
        }
}
